﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Infrastructure;
using Ones.Dal.Repository.Impl;
using Ones.Infrastructure.Utils.Pagination.Impl;

namespace Ones.Wcf.WebAdministration
{
    public class ServiceCategoryManagement : IServiceCategoryManagement
    {
        private ICategoryDao _dao;

        #region Constructor
        
        public ServiceCategoryManagement()
        {
            _dao = new CategoryDao();
        }
        
        #endregion
        
        #region DMM
        
        public bool
        addCategory(Category category)
        {
            return saveCategory(category, true);
        }
        
        public bool
        updateCategory(Category category)
        {
            return saveCategory(category, false);
        }

        public bool
        deleteCategory(string catId)
        {
            return _dao.delete(catId);
        }
        
        #endregion
        
        #region DQM
        
        public List<Category>
        searchAllCategories()
        {
            return _dao.getAll();
        }
        
        public Category
        searchCategoryById(string catId)
        {
            return _dao.getById(catId);
        }
        
        public List<Category>
        searchCategoryByName(string catName)
        {
            return _dao.getByName(catName);
        }
        
        public List<Category>
        searchCategoryByParent(string catId)
        {
            return _dao.getByCategory(catId);
        }
        
        public List<Category>
        searchCategoryByDivision(string divId)
        {
            return _dao.getByDivision(divId);
        }
        
        #endregion
        
        #region ///Paging///
        
        public List<Category>
        searchCategory(string searchBy, string keyword)
        {
            try
            {
                List<Category> lst = null;
                if (searchBy == "id")
                {
                    lst = _dao.getMultiple(e => e.catParentId.Contains(keyword) || e.belongToDivision.Contains(keyword));
                }
                else if (searchBy == "name")
                {
                    lst = _dao.getMultiple(e => e.catName.Contains(keyword));
                }
                return lst;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public PagingObject<Category>
        getCategories(int page)
        {
            try
            {
                //??? fixed value ?
                int numberOfObjectsPerPage = 10;
                
                var q = _dao.getAll();
                PagingObject <Category> obj = new PagingObject<Category>();
                obj.Data = q.Skip(numberOfObjectsPerPage * (page - 1))
                            .Take(numberOfObjectsPerPage)
                            .ToList();
                
                obj.TotalPage = q.Count() / numberOfObjectsPerPage;
                
                if (q.Count() % numberOfObjectsPerPage > 0)
                {
                    obj.TotalPage = obj.TotalPage + 1;
                }
                
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public LazyPagination<Category>
        pagingAllCategories(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _dao.getAll().AsQueryable();
                return new LazyPagination<Category>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        #endregion

        private bool
        saveCategory(Category category, bool isCreate)
        {
            if (String.IsNullOrEmpty(category.catParentId) && String.IsNullOrEmpty(category.belongToDivision))
            {
                return false;
            }
            else
            {
                //prepare info
                if (!String.IsNullOrEmpty(category.catParentId))
                {
                    Category parent = _dao.getById(category.catParentId);
                    category.belongToDivision = parent.belongToDivision;
                }
                
                //save to database
                if (isCreate)
                {
                    return _dao.create(category);
                }
                else
                {
                    return _dao.update(category);
                }
            }
        }
    }
}